import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

let router =  new Router({
    linkActiveClass:'active',
    routes: [
        {
            path:'/',
            redirect:'/main/movie/hot'
        },
        {
            path:'/main',
            component:() => import('@/views/main/index'),
            children:[
                {
                    path:'/main/movie',
                    redirect:'/main/movie/hot',
                    component:() => import('@/views/main/movie/index'),
                    children:[
                        {
                            path:'/main/movie/hot',
                            component:() => import('@/views/main/movie/hot'),
                        },
                        {
                            path:'/main/movie/will',
                            component:() => import('@/views/main/movie/will'),
                        }
                    ]
                },
                {
                    path:'/main/cinema',
                    component:() => import('@/views/main/cinema'),
                },
                {
                    path:'/main/my',
                    name:'my',
                    component:() => import('@/views/main/my'),
                }
            ]
        },
        {
            path:'/login',
            component:() => import('@/views/login')
        },
        {
            path:'/detail/:id',
            component:() => import('@/views/detail')
        }
    ]
})


let arr = ['my'];

router.beforeEach((to,from,next) => {
    if(arr.includes(to.name)){
        let uid = localStorage.getItem('username') || '';
        if(uid){
            next();
        }else{
            next('/login')
        }
    }else{
        next()
    }
})


export default router


